/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : SequenceProcessHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Nov 13, 2008       Raghu Sarraff			       Created
 */
package net.lwf.handler.math.sequence;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.entities.SequenceData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.common.constant.ICommonConstant;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.util.SequenceGenerator;
import net.lwf.number.util.RandomNumberGenerator;
import net.lwf.string.util.StringManipulator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;


/**
 * Type the class description here
 *  
 */
public class SequenceProcessHandler extends ProcessHandler implements IProcessHandler {
	
	private String grade;
	private String type;
	
	public SequenceProcessHandler(String grade, String type) {
		this.grade = grade;
		this.type = type;
	}

	public DisplayFormatter getDisplayFormatter() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getQuestionDesc() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}
	
	private QuestionInfo generateSingle(boolean isMultiple) {
		QuestionInfo qinfo = new QuestionInfo();
		SequenceData seqdata = SequenceGenerator.generateSequnce(this.type);
		qinfo.setAnswer(seqdata.getAnswer());
		if(!isMultiple) {
			qinfo.setHtmlQuestion(this.generateHtmlQuestion(seqdata));
			//Set Radio Answers
			qinfo.setRadioAnswerChoices(this.generateAnswerChoices(seqdata.getAnswer()));
		}
		else {
			qinfo.setQuestion(this.generateQuestion(seqdata));
		}
		qinfo.setPlaceHolder("Find the missing in the following " + seqdata.getName() + ICommonConstant.PERIOD); //Using the placeholder to store the sequence name
		return qinfo;
	}
	
	private String generateHtmlQuestion(SequenceData seqdata) {
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table border=\"0\" align=\"center\" cellspacing=\"2\" cellpadding=\"0\">");
	    	sbQBlock.append("<tr>");
	        	for(int i=0; i<seqdata.getSequence().size(); i++) {
	        		String data =seqdata.getSequence().get(i);
	        		if(data.trim().equals("?")) {
	        			sbQBlock.append("<td class=\"question\">");
	        		}
	        		else {
	        			sbQBlock.append("<td class=\"data\">");
	        		}
	        		sbQBlock.append(data);
	        		sbQBlock.append("</td>");
	        		sbQBlock.append("<td class=\"data\">&nbsp;</td>");
	        	}
	    	sbQBlock.append("</tr>");

        sbQBlock.append("</table>");
		return sbQBlock.toString();	
	}
	
	private String generateQuestion(SequenceData seqdata) {
		StringBuffer sbQBlock = new StringBuffer();
    	for(int i=0; i<seqdata.getSequence().size(); i++) {
    		String data =seqdata.getSequence().get(i);
    		sbQBlock.append(data);
    		sbQBlock.append(", ");
    	}
		return sbQBlock.toString();	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		List<QuestionInfo> questions = new ArrayList<QuestionInfo>();
		int nQuestions = getRows() * getColumns();
		for(int i=0; i<nQuestions; i++) {
			QuestionInfo question = generateSingle(true);
			question.setQuestionNumber(i+1);
			questions.add(question);
		}
		return questions;
	}
	
	private List<RadioData> generateAnswerChoices(String baseAnswer) {
		List<RadioData> data = new ArrayList<RadioData>();
		RadioData rdata = new RadioData(baseAnswer, baseAnswer);
		data.add(rdata);
		
		StringTokenizer stk = new StringTokenizer(baseAnswer, ICommonConstant.COMMA);
		//Additional choices
		List<Integer> choices = RandomNumberGenerator.generateIntsWithVariance(Integer.parseInt(stk.nextToken().trim()), 5, stk.countTokens()+1);
		
		String choice = StringManipulator.generateCsvString(choices);
		rdata = new RadioData(choice, choice);
		data.add(rdata);
		
		if(stk.countTokens() != 0) {
			//Reverse base answer string
			String reverse_answer = StringManipulator.reverse(baseAnswer);
			rdata = new RadioData(reverse_answer, reverse_answer);
			data.add(rdata);
			//Reverse answer choices
			choice = StringManipulator.reverse(choice);
			rdata = new RadioData(choice, choice);
			data.add(rdata);			
		}

		
		Collections.shuffle(data);
		return data;
	}
	
	@Override
	public void getContent(Document document) throws DocumentException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getRows() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getColumns() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean isPortrait() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getPdfNote() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
